package com.hengfei.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.hengfei.dao.TOrdersettingMapper;
import com.hengfei.pojo.OrderSetting;
import com.hengfei.pojo.TOrdersetting;
import com.hengfei.pojo.TOrdersettingExample;
import com.hengfei.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private TOrdersettingMapper tOrdersettingMapper;
    //添加数据
    @Override
    public void add(List<TOrdersetting> data) {
        for (TOrdersetting orderSetting : data) {
            //判断指定日期数据，在数据库中是否已存在
            if(orderdateIsExist(orderSetting.getOrderdate())) {
                //执行修改。根据日期，修改number
                tOrdersettingMapper.updateNumberByOrderdate(orderSetting);
            } else {
                //插入一条新数据
                tOrdersettingMapper.insertSelective(orderSetting);
            }
        }
    }

    //根据月份查询对应的预约设置数据
    @Override
    public List<Map> getOrderSettingByMonth(String date) {  //格式 YYYY-MM

        //调用DAO，根据日期范围查询预约设置数据
        TOrdersettingExample example = new TOrdersettingExample();
        //求orderDate的上下界
        Date[] dates = strToDate(date);
        //条件查询
        example.createCriteria().andOrderdateBetween(dates[0], dates[1]);
        List<TOrdersetting> tOrdersettings = tOrdersettingMapper.selectByExample(example);

        List<Map> result = new ArrayList<>();
        if(tOrdersettings != null && tOrdersettings.size() > 0){
            for (TOrdersetting tOrdersetting : tOrdersettings) {
                Map<String,Object> m = new HashMap<>();
                m.put("date",tOrdersetting.getOrderdate().getDate());//获取日期数字（几号）
                m.put("number",tOrdersetting.getNumber());
                m.put("reservations",tOrdersetting.getReservations());
                result.add(m);
            }
        }
        return result;
    }

    //修改可预约人数
    @Override
    public void editNumberByDate(TOrdersetting orderSetting) {
        //先查询数据库中是否已存在指定日期的数据
        if(orderdateIsExist(orderSetting.getOrderdate())) {
            //如果存在，就修改
            tOrdersettingMapper.updateNumberByOrderdate(orderSetting);
        } else {
            //如果不存在，就插入
            tOrdersettingMapper.insertSelective(orderSetting);
        }

    }


    /**
     * 将 2019-6 样式的字符串转成Date
     * @param strDate 形式为 2019-6 样式的字符串
     * @return 返回一个Date数组，内含两个元素。  0索引： 当前月份的开始     1 索引： 下一月份的开始
     */
    public Date[] strToDate(String strDate){
        String[] splitStr = strDate.split("-");
        int year = Integer.parseInt(splitStr[0]);
        int month = Integer.parseInt(splitStr[1]);
        Calendar c = Calendar.getInstance();
        c.set(year, month - 1, 1, 0, 0, 0);
        Calendar c2 = Calendar.getInstance();
        c2.set(year, month, 1, 0, 0, 0);

        return new Date[]{new Date(c.getTimeInMillis()), new Date(c2.getTimeInMillis())};
    }


    //判断日期数据在数据库中是否存在
    public boolean orderdateIsExist(Date date){
        //看该日期的数据是否存在，如果存在，就修改，否则，就插入
        TOrdersettingExample example = new TOrdersettingExample();
        //规定查询条件，日期等于orderSetting的日期
        example.createCriteria().andOrderdateEqualTo(date);
        int rows = tOrdersettingMapper.countByExample(example);
        //如果日期已存在，那么rows == 1，否则 rows == 0
        if(rows > 0) {
            return true;
        } else {
            return false;
        }
    }



}
